$(function () {
    var userId=$(".container .left .introduction .head-div .name").attr("name");

    var fanCount=parseInt($("#fanCount").text());
    var objectId=$("#articleContentId").attr("name");

    $(".container .left .introduction .data3 .follow").click(function () {
        var follow=$(this);
        $.ajax({
            type:"post",
            async:"true",
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            dataType: "json",//预期服务器返回的数据类型
            url: "/yuanwen/follow",
            data: {"targetId":userId},
            success: function (result) {
                if (result.code == 200) {
                    if(follow.text()=="关注"){
                        follow.text("已关注");
                        follow.css("color","#999aaa")
                        $("#fanCount").text(++fanCount);
                    }else {
                        $("#fanCount").text(--fanCount);
                        follow.text("关注");
                        follow.css("color","#555666")
                    }
                }
            }
        });
    });

    $(".container .right .article-header-box .article-info-box .bar-content .like").click(function () {
        var like=$(this);
        var likeCount=parseInt(like.children("span").text());
        var userlikeCount=parseInt($("#likeCount").text());
        $.ajax({
            type:"post",
            async:"true",
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            dataType: "json",//预期服务器返回的数据类型
            url: "/yuanwen/like",
            data: {"targetId":objectId},
            success: function (result) {
                if (result.code == 200) {
                    console.log(like.attr("name"));
                    if(like.attr("name")=="true"){
                        like.children(":first").attr("src","/img/点赞.png");
                        like.children("span").text(--likeCount);
                        $("#likeCount").text(--userlikeCount)
                        like.attr("name","false");
                    }else {
                        like.children(":first").attr("src","/img/已点赞.png");
                        like.children("span").text(++likeCount);
                        $("#likeCount").text(++userlikeCount)
                        like.attr("name","true");
                    }
                }
            }
        });
    });




    $(".container .favorites-div .head span").click(function () {
        $(".container .favorites-div").css({"display":"none"});
    })


    $(".container .favorites-div .collection-items li").click(function () {
        $(this).siblings('li').removeClass('select');  // 删除其他兄弟元素的样式
        $(this).addClass('select');                            // 添加当前元素的样式
    })

    $(".container .favorites-div .collection-folder .collection-input button").click(function () {

        var text=$(".container .favorites-div .collection-folder .collection-input input").val();
        $(".container .favorites-div .collection-folder .collection-input input").val("");
        if(text.replace(/(^s*)|(s*$)/g, "")!=""){

            $.ajax({
                url: '/yuanwen/insertFolder',
                async:"true",
                method: "get",
                contentType: 'application/json;charset=UTF-8',
                data: {'folderName': text},
                success: function (data) {
                    if (data === null) {
                    } else {
                        var newli=$("<li><span>"+text+"</span></li>");
                        newli.attr("name",data.objectid);
                        $(".container .favorites-div .collection-items").append(newli);
                        newli.click(function () {
                            $(this).siblings('li').removeClass('select');  // 删除其他兄弟元素的样式
                            $(this).addClass('select');                            // 添加当前元素的样式
                        })

                    }
                }
            });

        }
    });




    $(".container .favorites-div .collection-footer button").click(function () {
        var yuanwenFavorCount= parseInt($(".container .right .shoucang").children(':last-child').text());
        var userFavorCount=parseInt($("#favoriteCount").text());
        var seleli=$(".container .favorites-div .collection-items .select");
        var favorite;
        var isfav=$(".container .right .shoucang").attr("name");
        if(isfav=="false"){

            if(seleli!=null){
                if(seleli.children("span").text()=="默认收藏夹"){
                    favorite="默认收藏夹";
                }else {
                    favorite=seleli.attr("name");
                }
                $.ajax({
                    url: '/yuanwen/collect',
                    async:"true",
                    method: "get",
                    contentType: 'application/json;charset=UTF-8',
                    data: {'objectId': objectId,'favorite':favorite},
                    success: function (data) {
                        if(data.code==200){
                            $(".container .favorites-div").css({"display":"none"});
                            $(".container .right .shoucang").attr("name","ture");
                            $(".container .right .shoucang").children("img").attr("src","/img/已收藏.png");
                            $(".container .right .shoucang :nth-child(2)").text("已收藏");
                            $(".container .right .shoucang").children(':last-child').text(++yuanwenFavorCount);
                            $("#favoriteCount").text(++userFavorCount);

                        }
                    }
                });
            }
        }

    });


    $(".container .right .shoucang").click(function () {
        var yuanwenFavorCount= parseInt($(".container .right .shoucang").children(':last-child').text());
        var userFavorCount=parseInt($("#favoriteCount").text());
        var isfav=$(".container .right .shoucang").attr("name");
        if(isfav=="false"){
            $(".container .favorites-div").css({"display":"block"});
        }else {

            $.ajax({
                url: '/yuanwen/noCollect',
                async:"true",
                method: "get",
                contentType: 'application/json;charset=UTF-8',
                data: {'objectId': objectId},
                success: function (data) {
                    if(data.code==200){
                        $(".container .right .shoucang").attr("name","false");
                        $(".container .right .shoucang").children("img").attr("src","/img/收藏.png");
                        $(".container .right .shoucang").children(':last-child').text(--yuanwenFavorCount);
                        $(".container .right .shoucang :nth-child(2)").text("收藏");
                        $("#favoriteCount").text(--userFavorCount);
                    }
                }
            });
        }

    })


})

